package com.anysoft.web;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebView;

import com.anysoft.R;
import com.anysoft.WebViewNetworkProtocol;
import com.anysoft.base.BaseWebviewWithCacheActivity;
import com.anysoft.event.TimeOutEvent;
import com.anysoft.utils.ActivityUtils;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.HttpUtils;
import com.anysoft.utils.MentorLogger;
import com.anysoft.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import butterknife.OnClick;

import static com.anysoft.HostConstant.HOST_WEB_URL_HEADER;

/**
 * 理财视点
 * Created by wyb on 2017/3/26.
 */

public class FinancialServicesActivity extends BaseWebviewWithCacheActivity {

    private String id;
    private String url;
    private String head;
    private String shareUrl;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
//        SharePopup.setOnShareEventLinstener(new OnShareEventLinstenerImpl() {
//            @Override
//            public void onShareSuccess() {
//                AnalyticsEventUtils.toShareNewsEvent(FinancialServicesActivity.this);
//            }
//        });
    }

    private void initData() {
        title.setText("新闻资讯");
        isShowTitleBar(true);
        shareButtonTitle1.setVisibility(View.VISIBLE);
        if (getIntent() == null) {
            finish();
        }
        if (getIntent().hasExtra("id")) {
            id = getIntent().getStringExtra("id");
        }
        if (getIntent().hasExtra("url")) {
            url = getIntent().getStringExtra("url");
        }
        if (getIntent().hasExtra("head")) {
            head = getIntent().getStringExtra("head");
        }
        if (!StringUtils.isNullOrEmpty(head)) {
            title.setText(head);
        }
        if (StringUtils.isNullOrEmpty(url)) {
            initUrl("financialServices", HttpUtils.getCommonParams2StringByWeb(mActivity) + "&id=" + id + "&cusNo=" +
                    getFundCustomerNo());
        } else {
            Map<String, String> map = handleUrlParams(url);
            if (map != null) {
                id = map.get("id");
            }
            loadUrl = url + "&cusNo=" + getFundCustomerNo() + HttpUtils.getCommonParams2StringByWeb(mActivity);
        }
        shareUrl = HOST_WEB_URL_HEADER + "financialServices.html?isShare=1&id=" + id;
        if (CommonUtil.isNetWorkConnected(mActivity)) {
            initView(loadUrl);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onshouldOverrideUrlLoadingCallBack(WebView webview, String web_url) {
        MentorLogger.Log(TAG, "url=" + web_url);
        if (web_url.contains("prompt.html")) {
            mIntent.setClass(mActivity, SelectWarmPromptActivity.class);
            mIntent.putExtra("url", web_url);
            startActivity(mIntent);
        } else if (web_url.contains("fundDetail")) {
            String url = null;
            try {
                url = URLDecoder.decode(web_url, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            MentorLogger.Log(TAG, "url=" + url);
            String fundcode;
            String fundType;
            String mainTitle;
            String subTitle;
            Map<String, String> map = new HashMap<>();
            String[] params = url.split("\\?")[1].split("&");
            for (int i = 0; i < params.length; i++) {
                //System.out.println("params--" + params[i]);
                String key = params[i].split("=")[0];
                String value = "";
                try {
                    value = params[i].split("=")[1];
                } catch (Exception exception) {
                    MentorLogger.Log(TAG, "value为null");
                }
                map.put(key, value);
            }
            fundcode = map.get("fundCode");
            fundType = map.get("fundType");
            mainTitle = map.get("fundName") + "  " + map.get("fundCode");
            subTitle = map.get("fundstateDescription");

            mIntent.setClass(mActivity, FundDetailActivity.class);
            mIntent.putExtra("fundcode", fundcode);
            mIntent.putExtra("fundType", fundType);
            mIntent.putExtra("fundname", map.get("fundName"));
            mIntent.putExtra("mainTitle", mainTitle);
            mIntent.putExtra("subTitle", subTitle);
            startActivity(mIntent);

//            mIntent.putExtra("loadUrl", url);
//            mIntent.setClass(mActivity, NormalWebActivity.class);
//            startActivity(mIntent);
        } else if (web_url.contains(WebViewNetworkProtocol.GOTO_LOGIN)) {
            ActivityUtils.gotoLogin(mActivity, hasUser(), false, findUserId());
            EventBus.getDefault().post(new TimeOutEvent(true));
        } else if (url.contains(WebViewNetworkProtocol.GO_QUESTION)) {
            startRiskActivity(null);
        } else {
            webview.loadUrl(web_url);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTimeOutCallBack(TimeOutEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }

    @OnClick({R.id.shareButtonTitle1})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shareButtonTitle1:
                //TODO 需要H5对应连接地址
//                SharePopup.Share(mActivity, "" + head, shareUrl, "点击查看详情");

                break;
            default:
                super.onClick();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
